#############################################################################
# Preliminary regressions (homogeneous coefficients)
# Baseline data
#
#############################################################################

rm(list=ls())
graphics.off()

library(data.table)
library(fixest)
library(xtable)

rm(list=ls())
load("Data_new/Data_baseline.RData")
save_res = "Results"
time0=Sys.time()

#############################################################################
# Regressions with homogeneous slopes, errors clustered at the state level
#############################################################################

# Regression of yields on temperature and precipitation
f1 = feols(yield ~  precr + ddr ,data=dt, cluster="state")

# Regression of yields on temperature and precipitation, with county FE
f2 = feols(yield ~  precr + ddr | factor(fips) ,data=dt, cluster="state")

# Regression of yields on temperature and precipitation, with county and year FE
f3 = feols(yield ~  precr + ddr | factor(fips) + factor(year) ,data=dt, cluster="state")

# Regression of yields on temperature and precipitation, with county FE and state-year FE
f4 = feols(yield ~  precr + ddr | factor(fips) + factor(year)^factor(state),data=dt, cluster="state")

etable(f1,f2,f3,f4, file=paste0(save_res,"/Table_preliminar.tex"),
       keep=c("ddr","precr"),order=c("ddr","precr"),depvar=FALSE,
       signif.code=NA,digits="r3",fitstat=c("n"),tex=TRUE,replace=TRUE)


#############################################################################
# Regressions with homogeneous slopes, Drescoll and Kraay errors 2 lags
# but with ssc by default
#############################################################################

# Regression of yields on temperature and precipitation
f1s = feols(yield ~  precr + ddr ,data=dt, panel.id = ~fips+year ,vcov= DK(2))

# Regression of yields on temperature and precipitation, with county FE
f2s = feols(yield ~  precr + ddr | factor(fips) ,data=dt, panel.id = ~fips+year ,vcov= DK(2))

# Regression of yields on temperature and precipitation, with county and year FE
f3s = feols(yield ~  precr + ddr | factor(fips) + factor(year) ,data=dt, panel.id = ~fips+year ,vcov= DK(2))

# Regression of yields on temperature and precipitation, with county FE and state-year FE
f4s = feols(yield ~  precr + ddr | factor(fips) + factor(year)^factor(state),data=dt, panel.id = ~fips+year ,vcov= DK(2))


#############################################################################
# Regressions with homogeneous slopes, Drescoll and Kraay errors 3 lags
# but with ssc by default
#############################################################################

# Regression of yields on temperature and precipitation
f5s = feols(yield ~  precr + ddr ,data=dt, panel.id = ~fips+year ,vcov= DK(3))

# Regression of yields on temperature and precipitation, with county FE
f6s = feols(yield ~  precr + ddr | factor(fips) ,data=dt, panel.id = ~fips+year ,vcov= DK(3))

# Regression of yields on temperature and precipitation, with county and year FE
f7s = feols(yield ~  precr + ddr | factor(fips) + factor(year) ,data=dt, panel.id = ~fips+year ,vcov= DK(3))

# Regression of yields on temperature and precipitation, with county FE and state-year FE
f8s = feols(yield ~  precr + ddr | factor(fips) + factor(year)^factor(state),data=dt, panel.id = ~fips+year ,vcov= DK(3))

#############################################################################
# Regressions with homogeneous slopes, Drescoll and Kraay errors 5 lags
# but with ssc by default
#############################################################################

# Regression of yields on temperature and precipitation
f9s = feols(yield ~  precr + ddr ,data=dt, panel.id = ~fips+year ,vcov= DK(5))

# Regression of yields on temperature and precipitation, with county FE
f10s = feols(yield ~  precr + ddr | factor(fips) ,data=dt, panel.id = ~fips+year ,vcov= DK(5))

# Regression of yields on temperature and precipitation, with county and year FE
f11s = feols(yield ~  precr + ddr | factor(fips) + factor(year) ,data=dt, panel.id = ~fips+year ,vcov= DK(5))

# Regression of yields on temperature and precipitation, with county FE and state-year FE
f12s = feols(yield ~  precr + ddr | factor(fips) + factor(year)^factor(state),data=dt, panel.id = ~fips+year ,vcov= DK(5))

etable(f1s,f2s,f3s,f4s,f5s,f6s,f7s,f8s, file=paste0(save_res,"/Table_preliminar_DK_ssc_2lags.tex"),
       keep=c("ddr","precr"),order=c("ddr","precr"),depvar=FALSE,
       signif.code=NA,digits="r3",fitstat=c("n"),tex=TRUE,replace=TRUE)

time1=Sys.time()